package model

import (
	log "missC/utils/logs"
)

// Article 对应文字表
type Article struct {
	Id      int        `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:id" json:"id"`
	Title   string     `gorm:"column:title;type:varchar(100);comment:标题" json:"title"`
	Desc    string     `gorm:"column:desc;type:longtext;comment:描述" json:"desc"`
	Content string     `gorm:"column:content;type:longtext;comment:内容" json:"content"`
	Img     string     `gorm:"column:img;type:longtext;comment:图片" json:"img"`
	Open    uint       `gorm:"column:open;type:int(1) unsigned;comment:是否公开;NOT NULL" json:"open"`
	SortId  int        `gorm:"column:sort-id;type:int(11);comment:分类" json:"sortId"`
	Create  *LocalTime `gorm:"autoCreateTime;type:datetime;comment:创建时间" json:"create"`
	Update  *LocalTime `gorm:"autoUpdateTime;type:datetime;comment:更新时间" json:"update"`
}

// InsArt 文章添加
func InsArt(art *Article) int {

	db.Create(&art)

	log.Info("文章写入成功!", art.Id)
	return 200
}

// GetArt 查询文章列表
func GetArt(pageSize int, page int) ([]Article, int, int64) {
	var articleList []Article
	var err error
	var total int64
	//分页查询文章列表

	db.Offset((page-1)*pageSize).Limit(pageSize).Order("id desc").Select("id", "title", "desc", "sort-id", "open", "create", "update").Find(&articleList)
	// 统计有多少条数据
	db.Model(&articleList).Count(&total)
	if err != nil {
		return nil, 210, 0
	}
	log.Info("查询所有文章")
	return articleList, 200, total

}

// DelArt 文章删除
func DelArt(id int) int {
	db.Delete(&Article{}, id)
	log.Info("文章删除成功影响行id：", id)
	return 200
}

// UpArt 文章修改
func UpArt(art *Article) int {

	db.Model(&art).Updates(map[string]interface{}{"id": &art.Id, "title": &art.Title, "sort-id": &art.SortId, "open": &art.Open})

	log.Info("更新成功，受影响的id", art.Id)
	return 200
}
